<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Portal extends MX_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 * 	- or -
	 * 		http://example.com/index.php/welcome/index
	 * 	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	function Portal() {
		parent::__construct();
		$this->load->model('lookupmodel');
		$this->load->model('helpermodel');
		$this->load->model('portal_model');
		//check is logged in
		$this->load->model('languagemodel');
		$this->load->model('projects/mdl_projectsmodel');
		$this->language = $this->languagemodel->is_language_using();
		$this->load->helper('MY_url_helper');
		$this->load->helper('text');
	}

	public function index() {
		//SEO
		$this->lang->load('common/header', $this->language);
		$data['title'] = $this->lang->line('text_home_title');
		$data['description'] = $this->lang->line('text_home_description');
		$data['keywords'] = $this->lang->line('text_home_keywords');

		//refer to Int listing search
		$this->load->model('listingmap/mdl_listingmapmodel');

		//Is Lanscape or portrait
		$data['theme'] = $this->lookupmodel->getTheme($this->config->item('market'));
		$data['theme'] = $data['theme']->Value;

		//get Premium
		$data['premium_project'] = array();
		$data['media_project'] = array();
		$results = $this->lookupmodel->getPremiumProjectsV2($this->config->item('config_language_id'), $this->config->item('market'), 'Premium', 'Cover', $data['theme']);
		
		if($results) {
			foreach ($results as $result) {
				//$data['media_project'] = $this->mdl_projectsmodel->getPremiumImage($result->ID, 'Cover', 1);
				$data['media_project'] = $this->mdl_projectsmodel->getPremiumImageV2($result->ID, 'Cover', 1, $data['theme']);

				if($data['theme'] == IS_LANDSCAPE) {
					$dimension = LANDSCAPE_PREMIUM_DIMENSION;
				} else {
					$dimension = PORTRAIT_PREMIUM_DIMENSION;
				}

				$dimension = substr($dimension, 1);
				$dimension = explode('x', $dimension);
				$width = $dimension[0];
				$height = $dimension[1];
					
				$data['premium_project'][] = array(
					'ID'			=> $result->ID,
					'ProjectName'	=> $result->ProjectName,
					'proMedia'		=> $data['media_project'],
					'href'			=> base_url() . 'project-detail/' . $result->Link,
					'width'			=> $width,
					'height'		=> $height
				);
			}
		} else {
			$data['media_project'] = false;
		}

		//get Feature project
		$data['featured_projects'] = array();
		//$results = $this->lookupmodel->getFeaturedProjects($this->config->item('config_language_id'), $this->config->item('market'), 'Featured');
		$results = $this->lookupmodel->getFeaturedProjectsV2($this->config->item('config_language_id'), $this->config->item('market'), 'Featured', 'Cover', $data['theme']);
		if($results) {
			foreach ($results as $result) {
				//$data['media_project'] = $this->mdl_projectsmodel->getPremiumImage($result->ID, 'Cover', 1);
				$data['media_project'] = $this->mdl_projectsmodel->getPremiumImageV2($result->ID, 'Cover', 1, $data['theme']);

				if($data['theme'] == IS_LANDSCAPE) {
					$dimension = LANDSCAPE_FEATURED_DIMENSION;
				} else {
					$dimension = PORTRAIT_FEATURED_DIMENSION;
				}

				$dimension = substr($dimension, 1);
				$dimension = explode('x', $dimension);
				$width = $dimension[0];
				$height = $dimension[1];

				$data['featured_projects'][] = array(
					'ID'			=> $result->ID,
					'ProjectName'	=> $result->ProjectName,
					'Description'	=> $result->Description,
					'media_project'	=> $data['media_project'],
					'href'			=> base_url() . 'project-detail/' . $result->Link,
					'width'			=> $width,
					'height'		=> $height
				);
			}
		} else {
			$data['featured_projects'] = false;
		}
		
		//get Featured Listings
		$data['featured_listings'] = array();
		$results = $this->lookupmodel->getFeaturedPropertyListing($this->config->item('config_language_id'), $this->config->item('market'), 'Featured', 'Cover', $data['theme']);
		if($results) {
			foreach ($results as $result) {
				//$data['media_project'] = $this->mdl_projectsmodel->getPremiumImage($result->ID, 'Cover', 1);
				//$data['media_project'] = $this->mdl_projectsmodel->getPremiumImageV2($result->ID, 'Cover', 1, $data['theme']);
				$data['media_project'] = $this->mdl_listingmapmodel->get_media($result->Listing_ID, "Cover", 1);
		
				if($data['theme'] == IS_LANDSCAPE) {
					$dimension = LANDSCAPE_PREMIUM_DIMENSION;
				} else {
					$dimension = PORTRAIT_PREMIUM_DIMENSION;
				}
		
				$dimension = substr($dimension, 1);
				$dimension = explode('x', $dimension);
				$width = $dimension[0];
				$height = $dimension[1];
		
				$data['featured_listings'][] = array(
					'ID'			=> $result->Listing_ID,
					'ProjectName'	=> $result->ProjectName,
					'Description'	=> $result->Description,
					'proMedia'		=> $data['media_project'],
					'href'			=> base_url() . 'detail/' . $result->Link,
					'width'			=> $width,
					'height'		=> $height
				);
			}
		} else {
			$data['featured_listings'] = false;
		}
		
		//get Event
		$this->load->model('events/mdl_events');
		$id = 0;
		$results = $this->mdl_events->getEvents($id, $this->config->item('config_language_id'), $this->config->item('market'));
		$data['events'] = array();
		$count = 0;
		if($results) {
			foreach ($results as $result) {
				$count++;
				$eventsMedia = $this->mdl_events->getEventsImage($result->Event_ID);
				if($eventsMedia) {
					$imageUrl = $this->config->item('HTTP_EVENT_MEDIA_IMAGE') . $eventsMedia[0]->FileName . '-120x70' . $eventsMedia[0]->Extension;
				} else {
					$imageUrl = $this->config->item('HTTP_IMAGE') . 'photos/calendar-default.jpg';
				}
					
				$data['events'][] = array(
					'Event_ID'	=> $result->Event_ID,
					'Title'		=> $result->Title,
					'Summary'	=> $result->Summary,
					'StartDate'	=> $result->StartDate,
					'StartTime'	=> $result->StartTime,
					'EndDate'	=> $result->EndDate,
					'EndTime'	=> $result->EndTime,
					'href'		=> base_url() . 'events/detail/' . $result->Event_ID,
					'Cover'		=> $imageUrl
				);
				
				if($count == 3) {
					break;
				}
			}
		}
		//get Research
		$this->load->model('research/mdl_research');
		$data['latestResearch'] = array();
		//get category research using slug
		$category_id = $this->mdl_research->getCategoryId('research');
		if($category_id) {
			$result = $this->mdl_research->getLatestResearch($category_id, $this->config->item('config_language_id'), $this->config->item('market'), 1);
			if(!empty($result->ImageUrl)) {
				$imageUrl = $this->config->item('HTTP_ARTICLE_MEDIA_IMAGE') . $result->ImageUrl;
			} else {
				$imageUrl = $this->config->item('HTTP_IMAGE') . 'photos/buyer-guide.png';
			}
			
			if($result) {
				$data['latestResearch'] = array(
					'research_id'	=>	$result->Post_ID,
					'title'			=>	$result->Title,
					'summary'		=>	$result->Summary,
					'imageUrl'		=>	$imageUrl,
					'href'        	=> 	base_url() . 'research/detail/' . $result->Link
				);
			} 
		}
		
		//get Investment Guide
		$this->load->model('research/mdl_research');
		$data['investment_guide'] = array();
		//get category research using slug
		$category_id = $this->mdl_research->getCategoryId('investment-guide');
		
		if($category_id) {
			$result = $this->mdl_research->getInvestmentGuide($category_id, $this->config->item('config_language_id'), $this->config->item('market'), 1);
			
			if(!empty($result->ImageUrl)) {
				$imageUrl = $this->config->item('HTTP_ARTICLE_MEDIA_IMAGE') . $result->ImageUrl;
			} else {
				$imageUrl = $this->config->item('HTTP_IMAGE') . 'photos/buyer-guide.png';
			}
			
			if($result) {
				$data['investment_guide'] = array(
					'investment_id'	=>	$result->Post_ID,
					'title'			=>	$result->Title,
					'summary'		=>	$result->Summary,
					'content'		=>  $result->PostContent,
					'imageUrl'		=>	$imageUrl
				);
			} 
		}
		
		//get video for Home page
		$result = $this->portal_model->getVideo('Home', $this->config->item('market'));
		$data['video'] = "";
		if($result) {
			$data['video'] = $result->Link;
		}
		
		$data['module'] = "portal";
		$data['view_file'] = "home";

		echo Modules::run('template/default_template', $data);
	}
	
	function message() {
		//SEO
		$this->lang->load('common/header', $this->language);
		$data['title'] = $this->lang->line('text_home_title');
		$data['description'] = $this->lang->line('text_home_description');
		$data['keywords'] = $this->lang->line('text_home_keywords');

		$data['text_heading'] = $this->lang->line('text_heading');

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text'      => '',
			'href'      => base_url()
		);

		$data['module'] = "portal";
		$data['view_file'] = "message";

		echo Modules::run('template/one_col', $data);
	}
	

	private function check_isvalidated() {
		if (!$this->session->userdata('validated')) {
			redirect('login');
		}
	}

	function curl_get_call($url) {
		$headers = array(
			'Accept: application/json',
			'Content-Type: application/json'
			);

			$handle = curl_init();
			curl_setopt($handle, CURLOPT_URL, $url);
			curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
			curt_setopt($handle, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
			curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);

			$response = curl_exec($handle);
			$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);

			echo $response; die();

			return $response;
	}

	function getHomeFeaturedImage() {
		$lang = 1;
		$tsql_callSP = '{call sp_getHomeFeaturedImage(?)}';

		$params = array(
		array($lang, SQLSRV_PARAM_IN)
		);

		$results = $this->db->exec_query($tsql_callSP, $params);

		$data = array();

		if($results) {
			while ($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC)) {
				$data[] = $row;
			}
		}

		return $data;
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */